package com.atguigu.jedis;

import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author: 叶子
 * @date: 2022/04/23 20:41
 **/
public class JedisDemo1 {
    public static void main(String[] args) {
        //创建Jedis对象
        Jedis jedis = new Jedis("192.168.126.128",6379);
        //测试
        String value = jedis.ping();
        System.out.println(value);
        jedis.close();
    }
    //操作key
    @Test
    public void demo1(){
        Jedis jedis = new Jedis("192.168.126.128",6379);
        jedis.setnx("name","梅新元");
        //获取
        String s = jedis.get("name");
        System.out.println(s);
        //设置多个key-value
        jedis.mset("id","1","addr","chengdu");
        List<String> values = jedis.mget("id", "addr");
        System.out.println(values);
        Set<String> keys = jedis.keys("*");
        for (String key : keys) {
            System.out.println(key);
        }
        jedis.close();
    }
    //操作list
    @Test
    public void demo2(){
        Jedis jedis = new Jedis("192.168.126.128",6379);
        jedis.lpush("key1","lucy","mary","jack");
        List<String> list = jedis.lrange("key1", 0, -1);
        System.out.println(list);
        jedis.close();
    }
    //操作set
    @Test
    public void demo3(){
        Jedis jedis = new Jedis("192.168.126.128",6379);
        jedis.sadd("names","lucy");
        jedis.sadd("names","marry");
        jedis.sadd("names","lili");
        Set<String> set = jedis.smembers("names");
        for (String s : set) {
            System.out.println(s);
        }
        jedis.close();
    }
    //操作hash
    @Test
    public void demo4(){
        Jedis jedis = new Jedis("192.168.126.128",6379);
        jedis.hset("girl","id","1");
        jedis.hset("girl","name","梅晓碧");
        jedis.hset("girl","sex","female");
        jedis.hset("girl","age","26");
        jedis.hset("girl","hobby","singing");
        String name = jedis.hget("girl", "name");
        Map<String, String> girl = jedis.hgetAll("girl");
        girl.entrySet().forEach(System.out::println);
        System.out.println(name);
        jedis.close();
    }
    @Test
    public void demo5(){
        Jedis jedis = new Jedis("192.168.126.128",6379);
        jedis.zadd("movie",90,"喜羊羊与灰太狼");
        jedis.zadd("movie",70,"狂蟒之灾");
        jedis.zadd("movie",80,"破镜重圆");
        Set<String> movie = jedis.zrange("movie", 0, -1);
        movie.forEach(System.out::println);
        jedis.close();
    }

}